package com.fzebu.fastboot.db.builder;

import com.fzebu.fastboot.annotations.form.Table;
import com.fzebu.fastboot.db.intl.AbstractSQLBuilder;
import com.fzebu.fastboot.util.ModeUtil;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSetTrans;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.UUID;

/**
 * @author Jack
 * @version 1.0.0
 * @create 2020/2/16 19:28
 */
public class InsertBuilder extends AbstractSQLBuilder {

    private static final int MODE_DATA_CREATOR = 1;
    private static final String MODE_DATA_CREATOR_TYPE = "0";
    private static final String MODE_UUID_KEY = "MODEUUID";

    private String modeUuidKey;

    private int modeId;

    public InsertBuilder() {}

    public InsertBuilder(Object object) {
        this.resolve(object);
    }

    public InsertBuilder modeId(int modeId) {
        this.modeId = modeId;
        return this;
    }

    public InsertBuilder table(String tableName) {
        this.setTableName(tableName);
        return this;
    }

    public InsertBuilder param(String key, Object val) {
        this.setParam(key, val);
        return this;
    }

    public InsertBuilder trans(RecordSetTrans trans) {
        this.setTrans(trans);
        return this;
    }

    @Override
    protected String buildSql(String table, Map<String, Object> paramMap) {
        StringBuilder sql = new StringBuilder("INSERT INTO ");
        sql
                .append(table).append("(")
                .append(StringUtils.join(paramMap.keySet(), ","))
                .append(") VALUES(");
        int size = paramMap.size();
        for (int i = 0; i < size; i++) {
            if (i == 0) {
                sql.append('?');
            } else {
                sql.append(",?");
            }
        }
        sql.append(')');
        return sql.toString();
    }

    public void resolve(Object object) {
        Class<?> clazz = object.getClass();
        Table annotation = clazz.getDeclaredAnnotation(Table.class);
        // 解析@Table相关参数
        if (annotation != null) {
            this.setTableName(annotation.table());
            this.modeId = annotation.modeId();
        } else {
            this.setTableName(clazz.getSimpleName());
        }
    }

    @Override
    protected void beforeBuildParams(Map<String, Object> paramMap) {
        // 判断是否是建模表
        if (this.modeId > 0) {
            paramMap.put("FORMMODEID", this.modeId);
            paramMap.put("MODEDATACREATER", MODE_DATA_CREATOR);
            paramMap.put("MODEDATACREATERTYPE", MODE_DATA_CREATOR_TYPE);
            paramMap.put("MODEDATACREATEDATE", LocalDate.now().toString());
            paramMap.put("MODEDATACREATETIME", LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
            this.modeUuidKey = UUID.randomUUID().toString();
            paramMap.put("MODEUUID", this.modeUuidKey);
        }
    }

    @Override
    public void processResult(boolean execResult) {
        if (execResult) {
            ModeUtil.resetDataShare(this.modeId, this.getTableName(), this.modeUuidKey);
        }
    }

    @Override
    public String getSqlType() { return "INSERT"; }
}
